package com.ucode.springboot.ueditor.baidu.config;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ucode.springboot.starter.fileStorage.FsProviderType;
import com.ucode.springboot.ueditor.baidu.ActionEnter;
import com.ucode.springboot.ueditor.baidu.ConfigManager;
import com.ucode.springboot.ueditor.baidu.hunter.FileManager;
import com.ucode.springboot.ueditor.baidu.upload.StorageManager;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

@Configuration
@EnableConfigurationProperties(UeditorProperties.class)
@ConditionalOnClass(ActionEnter.class)
//@ConditionalOnProperty(prefix="ueditor",value="enabled",matchIfMissing=true)
public class UeditorAutoConfiguration {

    private static Log log = LogFactory.get();
    
	@Autowired
	private UeditorProperties ueditorProperties;
	
	@Bean
	@ConditionalOnMissingBean(ActionEnter.class)
	public ActionEnter actionEnter(){
	    log.info("开始初始化百度编辑器:"+ueditorProperties.getConfig());
		ActionEnter actionEnter = new ActionEnter( new ConfigManager(ueditorProperties.getConfig()));
		return actionEnter;
	}
	
	@PostConstruct
	public void storagemanager(){
	    StorageManager.clientId = FileManager.clientId = ueditorProperties.getClientId();
	    FsProviderType fsProviderType =  FsProviderType.valueOf(ueditorProperties.getStorageType());
		StorageManager.fsProviderType = FileManager.fsProviderType = fsProviderType;
		FileManager.pageSize = ueditorProperties.getPageSize();
	}
	
}
